package com.qmerak.dao.impl;

import java.util.List;
import java.util.UUID;

import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.qmerak.dao.HibernateDao;
import com.qmerak.entity.OrgEntity;
import com.qmerak.entity.PostUnitEntity;

/**
 * @author Shaohong•Cheng
 * @mail shaohong726@126.com
 * @date 2013-5-19
 * @project WebTest
 */
@Repository
public class UnitDao extends HibernateDao<PostUnitEntity, String> {
	public PostUnitEntity add(PostUnitEntity entity) {
		entity.setId(UUID.randomUUID().toString());
		return super.add(entity);
	}

	@SuppressWarnings("unchecked")
	public List<PostUnitEntity> unitsOfOrg(OrgEntity orgEntity) {
		Session session = this.sessionFactory.openSession();
		List<PostUnitEntity> list = session
				.createQuery("from PostUnitEntity where unionID=?")
				.setParameter(0, orgEntity.getId()).list();
		session.close();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<PostUnitEntity> queryByCondition(String condition) {
		String sql = "from PostUnitEntity ";
		if (null != condition && !"".equals(condition)) {
			sql += " where " + condition;
		}
		Session session = this.sessionFactory.openSession();
		List<PostUnitEntity> list = session.createQuery(sql).list();
		session.close();
		return list;
	}

}
